package lab03.test12;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) {
        //加载驱动
        Connection connection=null;
        java.sql.Statement statement=null;
        ResultSet resultSet = null;
        //表名字：t_users
        //1)   查询所有记录
        String sql="select * FROM t_users";
        //2)   查询用户名为'admin'的记录
        String sql1="select * FROM t_users where username='admin'" ;
        //3)   查询用户名为'admin'，密码为'123456'的记录
        String sql2="select * from t_users where username='admin' and password='123456'";
        //4)   查询注册时间大于10000000的记录；
        String sql3="select * from t_users where register_datetime>10000000";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                connection= DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                        "root",
                        "123456"
                );
            } catch (SQLException e) {
                e.printStackTrace();
            }

            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            resultSet = statement.executeQuery(sql1);
            resultSet = statement.executeQuery(sql2);
            resultSet = statement.executeQuery(sql3);
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3) + " " + resultSet.getString(4));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
